package com.hzit.pay.center.service.impl;

import com.hzit.pay.center.constant.RedisKeyConstant;
import com.hzit.pay.center.mapper.MchInfoMapper;
import com.hzit.pay.center.model.MchInfo;
import com.hzit.pay.center.service.IMchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.concurrent.TimeUnit;

/**
 * 商户配置相关接口
 */
@Service
public class MchServiceImpl implements IMchService {

    @Autowired
    private MchInfoMapper mchInfoMapper;

    @Autowired
    private ValueOperations<String,Object> valueOperations;

    @Override
    public MchInfo queryMchInfoById(String mchId) {

        String redisKey = RedisKeyConstant.MCH_INFO + mchId;
        MchInfo mchInfo =  (MchInfo)valueOperations.get(redisKey);

        if (!ObjectUtils.isEmpty(mchInfo)){
            return mchInfo;
        }

        mchInfo = mchInfoMapper.selectByPrimaryKey(Long.parseLong(mchId));

        if (ObjectUtils.isEmpty(mchInfo)){
            return null;

        }

        valueOperations.set(redisKey,mchInfo,12, TimeUnit.HOURS);

        return mchInfo;

    }
}
